// pages/print/pay/pay.js
const utils = require("../../../utils/utils")
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        choosePay: '',
        list: [],
        price: '0.00',
        payPass: '',
        pswShow: false,
        once: 1,
        orderNo: '',
        showBtn: true,
        orderType: 1,
        getStyle: 1,
        addressInfo: {
            mobile: '',
            address: '',
            id: '',
            trueName: '',
            change: 0
        },
        ele: 0,
        firstOpen: 0,
        remark: '',
        isOut: -1,
        adv: []
    },
    chooseAddress() {
        if (utils.is_define(this.data.orderNo)) {
            utils.toast('订单已经生成无法更改，请直接进行支付')
            return false;
        }
        wx.navigateTo({
            url: '/pages/my/addressList/addressList?choose=1',
        })
    },
    changeStyle(e) {
        if (utils.is_define(this.data.orderNo)) {
            utils.toast('订单已经生成无法更改，请直接进行支付')
            return false;
        }
        var index = e.currentTarget.dataset.index;
        this.setData({
            getStyle: index == 1 ? '2' : '1',
            orderType: index == 1 ? '3' : '1'
        })
        // if (this.data.getStyle == 2) {
        //     if (this.data.addressInfo.change == 1) {
        //         this.circluPrice();
        //     }
        // }
    },
    pswShow() {
        this.setData({
            payPass: '',
            pswShow: true
        })
    },
    //设置个空白方法，阻止冒泡
    touchAnother() {

    },
    pswHide() {
        this.setData({
            pswShow: false
        })
    },
    payPass(e) {
        this.setData({
            payPass: e.detail.value
        })
    },
    changeIndex(e) {
        var code = e.currentTarget.dataset.code;
        this.setData({
            choosePay: code
        })
    },
    btnConfirm() {

    },
    //开始支付
    dyxx() {
        var that = this;
        if (that.data.orderType == 'delivery') {
            if (!utils.is_define(that.data.addressInfo.mobile)) {
                utils.toast('送货上门请先添加收货地址')
                return false;
            }
        }
        if (that.data.choosePay == '-1') {
            //余额
            app.isSetPassWord(function (res) {
                that.pswShow();
            })
        }
        if (that.data.choosePay == '1') {
            if (that.data.orderType == '521') {
                that.orderPayEle()
            } else {
                that.orderPay()
            }
        }
    },
    //支付  电子证件照
    orderPayEle() {
        var that = this;
        that.selectComponent("#loading").showLoadingCus('请稍候');
        that.setData({
            showBtn: false
        })
        var data = {
            orderNo: that.data.orderNo || '',
            paymentMethod: that.data.choosePay,
            payPwd: that.data.payPass,
            serviceItemId: that.data.serviceSubtypeId,
            fileName: that.data.fileName,
            fileUrlSingle: that.data.fileUrlSingle,
            fileUrl: that.data.fileUrl,
            clothes: that.data.clothes
        }
        utils.request('idPhotoOrder/createOrder', data, function (res) {
            if (utils.is_define(res.data.data)) {
                that.setData({
                    orderNo: res.data.data.orderNo
                })
            }
            if (res.data.code == '200') {
                if (that.data.choosePay == '-1') {
                    that.setData({
                        showBtn: true
                    })
                    that.selectComponent("#loading").hideLoadingCus();
                    wx.redirectTo({
                        url: '/pages/my/electronicDetail/electronicDetail?id=' + that.data.orderNo,
                    })
                }
                if (that.data.choosePay == '1') {
                    if (utils.is_define(res.data.data.payInfo)) {
                        qq.requestWxPayment({
                            url: res.data.data.payInfo,
                            referer: res.data.data.referer,
                            success(res) {
                                that.setData({
                                    showBtn: true
                                })
                                that.setZFD();

                            },
                            fail(res) {
                                that.setData({
                                    showBtn: true
                                })
                                that.selectComponent("#loading").hideLoadingCus();
                                utils.toast('支付失败，请重试')
                            }
                        })
                    }
                }
            } else {
                that.setData({
                    showBtn: true
                })
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg)
            }
        })
    },
    //支付  实时单、配送单
    orderPay() {
        var that = this;
        that.setData({
            showBtn: false
        })
        if (that.data.type == 'yyzx' && !utils.is_define(that.data.addressInfo.id)) {
            utils.toast('还没选择收货地址哦！')
            return false;
        }
        that.selectComponent("#loading").showLoadingCus('请稍候');
        var data = {
            machineQrCode: wx.getStorageSync('machineQrCode'),
            orderNo: that.data.orderNo || '',
            paymentMethod: that.data.choosePay,
            payPwd: that.data.payPass,
            taskOrderNo: that.data.taskNo,
            orderType: that.data.orderType,
            remarks: that.data.remark,
            addressId: that.data.addressInfo.id ? that.data.addressInfo.id : '0'
        }
        utils.request('printOrder/createOrder', data, function (res) {
            if (utils.is_define(res.data.data)) {
                that.setData({
                    orderNo: res.data.data.orderNo
                })
            }
            if (res.data.code == '200') {
                if (that.data.choosePay == '-1') {
                    that.setData({
                        showBtn: true
                    })
                    that.selectComponent("#loading").hideLoadingCus();
                    wx.redirectTo({
                        url: '../paySuccess/paySuccess?type=' + that.data.orderType + '&orderNo=' + that.data.orderNo+'&printType='+that.data.type,
                    })
                }
                if (that.data.choosePay == '1') {
                    if (utils.is_define(res.data.data.payInfo)) {
                        qq.requestWxPayment({
                            url: res.data.data.payInfo,
                            referer: res.data.data.referer,
                            success(res) {
                                that.setData({
                                    showBtn: true
                                })
                                that.setZF();
                            },
                            fail(res) {
                                that.setData({
                                    showBtn: true
                                })
                                that.selectComponent("#loading").hideLoadingCus();
                                utils.toast('支付失败，请重试')
                            }
                        })
                    }
                }
            } else {
                that.setData({
                    showBtn: true
                })
                that.selectComponent("#loading").hideLoadingCus();
                utils.toast(res.data.msg)
            }
        })
    },
    //查询支付（电子证件照）
    setZFD() {
        var that = this;
        var num = 1;
        that.data.setInter = setInterval(function () {
            num++;
            utils.request('idPhotoOrder/detail/' + that.data.orderNo, {}, function (res) {
                if (res.data.code == 200) {
                    if (res.data.data.orderStatus != 1) {
                        clearInterval(that.data.setInter)
                        that.selectComponent("#loading").hideLoadingCus();
                        wx.redirectTo({
                            url: '/pages/my/electronicDetail/electronicDetail?id=' + that.data.orderNo,
                        })
                    } else {
                        if (num == 5) {
                            clearInterval(that.data.setInter)
                            that.selectComponent("#loading").hideLoadingCus();
                            utils.toast('支付失败，请重试')
                        }
                    }
                } else {
                    clearInterval(that.data.setInter)
                    that.selectComponent("#loading").hideLoadingCus();
                    utils.toast(res.data.msg)
                }
            })
        }, 1000)
    },
    //查询支付(配送单，实时单)
    setZF() {
        var that = this;
        var num = 1;
        that.data.setInter = setInterval(function () {
            num++;
            utils.request('printOrder/detail/' + that.data.orderNo, {}, function (res) {
                if (res.data.code == 200) {
                    if (res.data.data.printOrder.orderStatus != 1) {
                        clearInterval(that.data.setInter)
                        that.selectComponent("#loading").hideLoadingCus();
                        wx.redirectTo({
                            url: '../paySuccess/paySuccess?type=' + that.data.orderType + '&orderNo=' + that.data.orderNo+'&printType='+that.data.type,
                        })
                    } else {
                        if (num == 5) {
                            clearInterval(that.data.setInter)
                            that.selectComponent("#loading").hideLoadingCus();
                            utils.toast('支付失败，请重试')
                        }
                    }
                } else {
                    clearInterval(that.data.setInter)
                    that.selectComponent("#loading").hideLoadingCus();
                    utils.toast(res.data.msg)
                }
            })
        }, 1000)
    },
    // 密码确定
    confirmPsw() {
        this.pswHide()
        if (this.data.orderType == '521') {
            this.orderPayEle()
        } else {
            this.orderPay()
        }
    },
    goUrl(e) {
        var that = this;
        app.get_local('AccToken').then(res => {
            if (utils.is_define(res)) {
                var url = e.currentTarget.dataset.url;
                if (url == '/pages/my/bindMobile/bindMobile') {
                    if (utils.is_define(that.data.mobile)) {
                        utils.toast("您已绑定，如需更换，请联系管理员")
                        return false;
                    } else {
                        wx.navigateTo({
                            url: url,
                        })
                    }
                } else {
                    wx.navigateTo({
                        url: url,
                    })
                }
            } else {
                wx.navigateTo({
                    url: '/pages/my/login/login',
                })
            }
        })
    },
    //获取支付方式
    getPayStyle() {
        var that = this;
        utils.request('config/getDictData/print_order:payment_method', {}, function (res) {
            that.setData({
                list: res.data.data,
                choosePay: res.data.data[0].dictValue
            })
        })
    },
    getBanlance() {
        var that = this;
        utils.request('user/userBalance', {}, function (res) {
            that.setData({
                balance: res.data.data.data
            })
        })
    },
    getAddress() {
        var that = this;
        utils.request('address/getDefaultAddress', {}, function (res) {
            if (res.data.code == 200) {
                if (utils.is_define(res.data.data.id)) {
                    that.setData({
                        ['addressInfo.id']: res.data.data.id,
                        ['addressInfo.trueName']: res.data.data.realName,
                        ['addressInfo.mobile']: res.data.data.tel,
                        ['addressInfo.address']: res.data.data.address + res.data.data.houseNumber,
                        ['addressInfo.change']: 0
                    })
                } else {
                    that.setData({
                        adv: []
                    })
                }
            } else {

            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.getPayStyle(); //获取支付方式
        that.getBanlance(); //获取余额
        if (wx.getStorageSync('virtualMachine') == 1) {
            that.setData({
                type: 'yyzx'
            })
            that.getAddress();
        }else{
            that.setData({
                type:''
            })
        }
        wx.getSystemInfo({
            success: (result) => {
                var model = result.model;
                if (model == 'iPhone X') {
                    that.setData({
                        pdbottom: '68',
                        price: options.price,
                        taskNo: options.taskNo,
                        orderType: options.orderType,
                        clothes: options.clothes || '',
                        fileName: options.fileName || '',
                        fileUrl: options.fileUrl || '',
                        fileUrlSingle: options.fileUrlSingle || '',
                        serviceSubtypeId: options.serviceSubtypeId || '',
                        orderNo: options.orderNo || ''
                    })
                } else {
                    that.setData({
                        pdbottom: '32',
                        price: options.price,
                        taskNo: options.taskNo,
                        orderType: options.orderType,
                        clothes: options.clothes || '',
                        fileName: options.fileName || '',
                        fileUrl: options.fileUrl || '',
                        fileUrlSingle: options.fileUrlSingle || '',
                        serviceSubtypeId: options.serviceSubtypeId || '',
                        orderNo: options.orderNo || ''
                    })
                }
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
    */
    onShow: function () {
        this.getBanlance()
        if (this.data.firstOpen == 1) {
            // if (utils.is_define(this.data.addressInfo.mobile) && this.data.addressInfo.change == 1 && this.data.getStyle == 2) {
            //     this.circluPrice()
            // }
        } else {
            this.setData({
                firstOpen: 1
            })
        }
    },
    circluPrice() {
        var that = this;
        if (!utils.is_define(that.data.addressInfo.id)) {
            return false;
        }
        wx.showLoading({
            title: '请稍候',
            mask: true
        })
        app.get_local('machineQrCode').then(res => {
            var data = {
                addressId: that.data.addressInfo.id,
                sn: res,
                remark: that.data.remark
            }
            utils.request('Order/getLogisticsMoney', data, function (res) {
                wx.hideLoading()
                if (res.data.code == 1) {
                    that.setData({
                        orderNoOnline: res.data.data.orderNo,
                        sendMoney: res.data.data.amount,
                        ['addressInfo.change']: 0
                    })
                    that.circluPriceOnce()
                } else {
                    that.setData({
                        orderNoOnline: '',
                        sendMoney: 0,
                        newPrice: that.data.price,
                        addressInfo: {
                            address: '',
                            trueName: '',
                            mobile: '',
                            id: '',
                            change: 0
                        }
                    })
                    utils.toast(res.data.msg)
                }
            })
        })
    },
    bindRemark(e) {
        this.setData({
            remark: e.detail.value
        })
    },
    circluPriceOnce() {
        var newPrice = parseFloat(this.data.price) + parseFloat(this.data.sendMoney)
        this.setData({
            newPrice: newPrice.toFixed(2)
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})